/**
 * 
 */
package com.terrence.filemgr;

import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import com.terrence.filemgr.mgr.AbsFileMgr;
import com.terrence.filemgr.mgr.AbsFileMgrContext;
import com.terrence.filemgr.mgr.IFileMgr;
import com.terrence.filemgr.mgr.impl.ftp.FTPContext;
import com.terrence.filemgr.mgr.impl.ftp.FTPMgr;
import com.terrence.filemgr.mgr.impl.localfs.LocalFSContext;
import com.terrence.filemgr.mgr.impl.localfs.LocalFSMgr;

/**
 * @author wuchuang
 *
 * 2016年10月28日 下午5:32:30
 * 
 * 附件处理工厂类
 */
public final class FileMgrFactory {
	
	private static FileMgrFactory factory = null;
	private final static Object lock = new Object();
	private Properties config;
	
	private FileMgrFactory(){
		reloadConfig();
	};
	
	public static FileMgrFactory getInstance(){
		if(null==factory){
			synchronized(lock){
				if(null==factory){
					factory = new FileMgrFactory();
				}
			}
		}
		return factory;
	}
	
	public IFileMgr build(String type){
		AbsFileMgr processor = null;
		AbsFileMgrContext context = null;
		if(!StringUtils.isEmpty(type)){
			switch(type){
			case "ftp":
				context = FTPContext.getInstance(getConfig());
				processor = new FTPMgr(context);
				break;
			case "local":
				context = LocalFSContext.getInstance(getConfig());
				processor = new LocalFSMgr(context);
				break;
			default:
				break;
			}
		}
		return processor;
	}

	private Properties getConfig() {
		return config;
	}

	private void setConfig(Properties config) {
		this.config = config;
	}	
	
	public void reloadConfig(){
		try{
			setConfig(FileMgrConfigLoader.load());
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
